/*     */ package com.zimbra.cs.dav.service.method;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.GalContact;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.GalMode;
/*     */ import com.zimbra.cs.account.Provisioning.SearchGalResult;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.Depth;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.DavException.REPORTwithDisallowedDepthException;
/*     */ import com.zimbra.cs.dav.property.Acl.Ace;
/*     */ import com.zimbra.cs.dav.resource.DavResource;
/*     */ import com.zimbra.cs.dav.resource.MailItemResource;
/*     */ import com.zimbra.cs.dav.resource.UrlNamespace;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import com.zimbra.soap.type.GalSearchType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AclReports
/*     */   extends Report
/*     */ {
/*     */   public void handle(DavContext ctxt)
/*     */     throws DavException, ServiceException
/*     */   {
/*  53 */     ctxt.setStatus(207);
/*  54 */     Element query = ctxt.getRequestMessage().getRootElement();
/*  55 */     if (query.getQName().equals(DavElements.E_PRINCIPAL_PROPERTY_SEARCH)) {
/*  56 */       handlePrincipalPropertySearch(ctxt, query);
/*  57 */     } else if (query.getQName().equals(DavElements.E_ACL_PRINCIPAL_PROP_SET)) {
/*  58 */       handleAclPrincipalPropSet(ctxt, query);
/*  59 */     } else if (query.getQName().equals(DavElements.E_PRINCIPAL_MATCH)) {
/*  60 */       handlePrincipalMatch(ctxt, query);
/*  61 */     } else if (query.getQName().equals(DavElements.E_PRINCIPAL_SEARCH_PROPERTY_SET)) {
/*  62 */       handlePrincipalSearchPropertySet(ctxt, query);
/*     */     } else {
/*  64 */       throw new DavException("msg " + query.getName() + " is not an ACL report", 400);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void handlePrincipalPropertySearch(DavContext ctxt, Element query)
/*     */     throws DavException, ServiceException
/*     */   {
/*  73 */     DavContext.RequestProp reqProp = ctxt.getRequestProp();
/*  74 */     DavResponse resp = ctxt.getDavResponse();
/*     */     
/*     */ 
/*     */ 
/*  78 */     resp.getTop(DavElements.E_MULTISTATUS);
/*  79 */     for (DavResource rs : getMatchingResources(ctxt, query)) {
/*  80 */       resp.addResource(ctxt, rs, reqProp, false);
/*     */     }
/*     */   }
/*     */   
/*     */   private ArrayList<DavResource> getMatchingResources(DavContext ctxt, Element query) throws DavException, ServiceException
/*     */   {
/*  86 */     ArrayList<DavResource> ret = new ArrayList();
/*  87 */     boolean applyToPrincipalCollection = query.element(DavElements.E_APPLY_TO_PRINCIPAL_COLLECTION_SET) != null;
/*  88 */     String path = ctxt.getUri();
/*  89 */     if ((!applyToPrincipalCollection) && (!path.startsWith("/principals/users/"))) {
/*  90 */       return ret;
/*     */     }
/*     */     
/*  93 */     GalSearchType type = GalSearchType.all;
/*  94 */     String queryType = query.attributeValue("type");
/*  95 */     if (queryType != null) {
/*  96 */       if (queryType.compareToIgnoreCase("INDIVIDUAL") == 0) {
/*  97 */         type = GalSearchType.account;
/*  98 */       } else if (queryType.compareToIgnoreCase("RESOURCE") == 0) {
/*  99 */         type = GalSearchType.resource;
/*     */       }
/*     */     }
/* 102 */     List propSearch = query.elements(DavElements.E_PROPERTY_SEARCH);
/* 103 */     for (Object obj : propSearch) {
/* 104 */       if ((obj instanceof Element))
/*     */       {
/* 106 */         Element ps = (Element)obj;
/* 107 */         Element prop = ps.element(DavElements.E_PROP);
/* 108 */         Element match = ps.element(DavElements.E_MATCH);
/* 109 */         if ((prop != null) && (match != null)) {
/* 110 */           Element e = (Element)prop.elements().get(0);
/* 111 */           ret.addAll(getMatchingPrincipals(ctxt, e.getQName(), match.getText(), type));
/*     */         }
/*     */       }
/*     */     }
/* 115 */     return ret;
/*     */   }
/*     */   
/*     */   private ArrayList<DavResource> getMatchingPrincipals(DavContext ctxt, QName prop, String match, GalSearchType type) throws DavException, ServiceException {
/* 119 */     Provisioning prov = Provisioning.getInstance();
/* 120 */     ArrayList<DavResource> ret = new ArrayList();
/* 121 */     Account authAccount = ctxt.getAuthAccount();
/* 122 */     if (prop.equals(DavElements.E_DISPLAYNAME)) {
/* 123 */       if ((!authAccount.isFeatureGalEnabled()) || (!authAccount.isFeatureGalAutoCompleteEnabled()))
/* 124 */         return ret;
/* 125 */       Provisioning.SearchGalResult result = prov.searchGal(prov.getDomain(authAccount), match, type, Provisioning.GalMode.zimbra, null);
/* 126 */       for (GalContact ct : result.getMatches()) {
/* 127 */         String email = (String)ct.getAttrs().get(ContactConstants.A_email);
/* 128 */         if (email != null) {
/* 129 */           Account acct = prov.get(Key.AccountBy.name, email);
/* 130 */           if (acct != null)
/* 131 */             ret.add(UrlNamespace.getPrincipal(ctxt, acct));
/*     */         }
/*     */       }
/* 134 */     } else if (prop.equals(DavElements.E_CALENDAR_HOME_SET)) {
/* 135 */       int index = match.lastIndexOf('/');
/* 136 */       if (index > 0)
/* 137 */         match = match.substring(index + 1);
/* 138 */       Account acct = prov.get(Key.AccountBy.name, match);
/* 139 */       if (acct != null)
/* 140 */         ret.add(UrlNamespace.getPrincipal(ctxt, acct));
/*     */     }
/* 142 */     return ret;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void handleAclPrincipalPropSet(DavContext ctxt, Element query)
/*     */     throws DavException, ServiceException
/*     */   {
/* 158 */     if (ctxt.getDepth() != DavContext.Depth.zero) {
/* 159 */       throw new DavException.REPORTwithDisallowedDepthException(query.getQName().getName(), ctxt.getDepth());
/*     */     }
/* 161 */     DavContext.RequestProp reqProp = ctxt.getRequestProp();
/* 162 */     DavResponse resp = ctxt.getDavResponse();
/*     */     
/*     */ 
/* 165 */     resp.getTop(DavElements.E_MULTISTATUS);
/* 166 */     for (DavResource rs : getAclPrincipals(ctxt))
/* 167 */       resp.addResource(ctxt, rs, reqProp, false);
/*     */   }
/*     */   
/*     */   private ArrayList<DavResource> getAclPrincipals(DavContext ctxt) throws DavException, ServiceException {
/* 171 */     ArrayList<DavResource> ret = new ArrayList();
/* 172 */     DavResource res = ctxt.getRequestedResource();
/* 173 */     if (!(res instanceof MailItemResource))
/* 174 */       return ret;
/* 175 */     List<Acl.Ace> aces = ((MailItemResource)res).getAce(ctxt);
/* 176 */     Provisioning prov = Provisioning.getInstance();
/* 177 */     for (Acl.Ace ace : aces) {
/* 178 */       if (ace.hasHref()) {
/* 179 */         Account acct = prov.get(Key.AccountBy.id, ace.getZimbraId());
/* 180 */         if (acct != null)
/* 181 */           ret.add(UrlNamespace.getPrincipal(ctxt, acct));
/*     */       }
/*     */     }
/* 184 */     return ret;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void handlePrincipalMatch(DavContext ctxt, Element query)
/*     */     throws DavException, ServiceException
/*     */   {
/* 193 */     if (ctxt.getDepth() != DavContext.Depth.zero) {
/* 194 */       throw new DavException.REPORTwithDisallowedDepthException(query.getQName().getName(), ctxt.getDepth());
/*     */     }
/* 196 */     ArrayList<DavResource> ret = new ArrayList();
/* 197 */     DavContext.RequestProp reqProp = ctxt.getRequestProp();
/* 198 */     DavResponse resp = ctxt.getDavResponse();
/*     */     
/*     */ 
/* 201 */     resp.getTop(DavElements.E_MULTISTATUS);
/* 202 */     Element principalProp = query.element(DavElements.E_PRINCIPAL_PROPERTY);
/* 203 */     if (principalProp == null)
/*     */     {
/* 205 */       String path = ctxt.getUri();
/* 206 */       if (path.startsWith("/principals/users/")) {
/* 207 */         ret.add(UrlNamespace.getPrincipal(ctxt, ctxt.getAuthAccount()));
/*     */       }
/*     */     } else {
/* 210 */       Element owner = principalProp.element(DavElements.E_OWNER);
/* 211 */       if (owner != null)
/*     */       {
/* 213 */         DavResource rs = ctxt.getRequestedResource();
/* 214 */         if (rs.isCollection())
/* 215 */           ret.addAll(rs.getChildren(ctxt));
/*     */       }
/*     */     }
/* 218 */     for (DavResource rs : ret) {
/* 219 */       resp.addResource(ctxt, rs, reqProp, false);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/* 225 */   private static final ArrayList<Pair<QName, Element>> PRINCIPAL_SEARCH_PROPERTIES = new ArrayList();
/* 226 */   static { addSearchProperty(DavElements.E_DISPLAYNAME, "Full name");
/* 227 */     addSearchProperty(DavElements.E_EMAIL_ADDRESS_SET, "Email Address");
/* 228 */     addSearchProperty(DavElements.E_CALENDAR_USER_TYPE, "User type");
/* 229 */     addSearchProperty(DavElements.E_CALENDAR_USER_ADDRESS_SET, "Calendar user address");
/* 230 */     addSearchProperty(DavElements.E_CALENDAR_HOME_SET, "Calendar home");
/*     */   }
/*     */   
/*     */   private static void addSearchProperty(QName prop, String desc) {
/* 234 */     Element elem = DocumentHelper.createElement(DavElements.E_DESCRIPTION);
/* 235 */     elem.addAttribute(DavElements.E_LANG, "en-us");
/* 236 */     elem.setText(desc);
/* 237 */     PRINCIPAL_SEARCH_PROPERTIES.add(new Pair(prop, elem));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void handlePrincipalSearchPropertySet(DavContext ctxt, Element query)
/*     */     throws DavException, ServiceException
/*     */   {
/* 247 */     if (ctxt.getDepth() != DavContext.Depth.zero) {
/* 248 */       throw new DavException.REPORTwithDisallowedDepthException(query.getQName().getName(), ctxt.getDepth());
/*     */     }
/* 250 */     Element response = ctxt.getDavResponse().getTop(DavElements.E_PRINCIPAL_SEARCH_PROPERTY_SET);
/* 251 */     ctxt.setStatus(200);
/* 252 */     for (Pair<QName, Element> prop : PRINCIPAL_SEARCH_PROPERTIES) {
/* 253 */       Element searchProp = response.addElement(DavElements.E_PRINCIPAL_SEARCH_PROPERTY);
/* 254 */       searchProp.addElement(DavElements.E_PROP).addElement((QName)prop.getFirst());
/* 255 */       searchProp.add(((Element)prop.getSecond()).createCopy());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/method/AclReports.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */